package com.stock.demo.function.selfselect.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.adapter.SelfSelectAdapter;
import com.stock.demo.model.StockBean;
import com.stock.demo.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by XuZhaoyu on 2017/7/1.
 */

public class DoubleRecyclerView extends LinearLayout {

    private RecyclerView mRvTitle;
    private RecyclerView mRvContent;
    private View mView;

    private TextView mTvEditBtn;
    private TextView mTvLastTrade;
    private TextView mTvChangePercent;
    private TextView mTvChange;
    private TextView mTvBid;
    private TextView mTvAsk;
    private TextView mTvPreClose;
    private TextView mTvOpen;
    private TextView mTvVolume;
    private TextView mTvTurOver;

    private ArrayList<StockBean> mData = new ArrayList<>();
    private SelfSelectAdapter mTitleAdapter;
    private SelfSelectAdapter mContentAdapter;


    public DoubleRecyclerView(Context context) {
        this(context, null);
    }

    public DoubleRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DoubleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initData();
    }

    private void initData() {
        mTvEditBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了编辑按钮");
            }
        });
        mTvLastTrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了最新");
            }
        });
        mTvChangePercent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了涨幅");
            }
        });
        mTvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了涨跌");
            }
        });
        mTvBid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了买入");
            }
        });
        mTvAsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了卖出");
            }
        });
        mTvPreClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了昨收");
            }
        });
        mTvOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了今开");
            }
        });
        mTvVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了成交量");
            }
        });
        mTvTurOver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了成交额");
            }
        });

        mRvTitle.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvContent.setLayoutManager(new LinearLayoutManager(getContext()));

        mTitleAdapter = new SelfSelectAdapter(getContext(), mData, SelfSelectAdapter.TYPE_TITLE);
        mContentAdapter = new SelfSelectAdapter(getContext(), mData, SelfSelectAdapter.TYPE_CONTENT);

        mRvTitle.setAdapter(mTitleAdapter);
        mRvContent.setAdapter(mContentAdapter);

        syncScroll(mRvTitle, mRvContent);

    }

    private void initView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.view_double_recycler_view, this, true);
        mRvTitle = (RecyclerView) mView.findViewById(R.id.rv_title);
        mRvContent = (RecyclerView) mView.findViewById(R.id.rv_content);
        mTvEditBtn = (TextView) mView.findViewById(R.id.tv_edit_btn);
        mTvLastTrade = (TextView) mView.findViewById(R.id.tv_head_last_trade);
        mTvChangePercent = (TextView) mView.findViewById(R.id.tv_head_change_percent);
        mTvChange = (TextView) mView.findViewById(R.id.tv_head_change);
        mTvBid = (TextView) mView.findViewById(R.id.tv_head_bid);
        mTvAsk = (TextView) mView.findViewById(R.id.tv_head_ask);
        mTvPreClose = (TextView) mView.findViewById(R.id.tv_head_pre_close);
        mTvOpen = (TextView) mView.findViewById(R.id.tv_head_open);
        mTvVolume = (TextView) mView.findViewById(R.id.tv_head_volume);
        mTvTurOver = (TextView) mView.findViewById(R.id.tv_head_tur_over);
    }

    private void syncScroll(final RecyclerView leftList, final RecyclerView rightList) {
        leftList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    rightList.scrollBy(dx, dy);
                }
            }
        });

        rightList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    leftList.scrollBy(dx, dy);
                }
            }
        });
    }

    public void setData(ArrayList<StockBean> data) {
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
            mTitleAdapter.notifyDataSetChanged();
            mContentAdapter.notifyDataSetChanged();
        }
    }

    public void addData(ArrayList<StockBean> data) {
        if (data != null && data.size() > 0) {
            mData.addAll(data);
            mTitleAdapter.notifyDataSetChanged();
            mContentAdapter.notifyDataSetChanged();
        }
    }

}
